#pragma once 

#include "tcpserver.hpp"
#include <iostream>
#include <signal.h>
#include <pthread.h>
#include "log.hpp"
#include "Task.hpp"
#include "ThreadPool.hpp"

class httpserver
{
  private:
    int port;
    bool stop;
  public:
    httpserver(int _port):port(_port),stop(false)
    {}
    ~httpserver()
    {}

    void Isitserver()
    {
      //SIGPIPE信号需要忽略，防止响应写入时server崩溃
      signal(SIGPIPE,SIG_IGN);
    }
    void loop()
    {
      tcpserver* tsvr=tcpserver::getinstance(port);

      LOG(INFO,"httpserver Isit 成功");
        LOG(INFO,"httpserver loop 成功");
      while(!stop)
      {
        struct sockaddr_in peer;
        socklen_t len=sizeof(peer);
        int sock=accept(tsvr->Sock(),(struct sockaddr*)&peer,&len);
        if(sock<0)
        {
          continue;
        }
        Task task(sock);
        ThreadPool::getinstance()->PushTask(task);
      }
    }
};
